---
title: "Replication Data for The Limited Influence of Right-Wing Movements on Social Media User Engagement"
author: "Carsten Schwemmer"
output: html_document
---

*Disclaimer: This notebook includes the R code used for creating tables and figures for the main paper as well as the appendix. The input is an .RData file containing a structural topic model and corresponding other objects for Pegida posts. Unfortunately, I am not allowed to publicly share the data files required for replication. In case you would like to get access, please reach out to me at c.schwem2er@gmail.com*

## Setup

Packages: 

```{r message=FALSE, warning=FALSE, results=}
library(MASS)
library(sjPlot)
library(ggeffects)
library(tidyverse)
library(lubridate)
library(scales)
library(stm)
library(ggplot2)
library(quanteda)
library(texreg)
library(stminsights)
sessionInfo()
```

Data and auxiliary functions:

```{r}
theme_set(theme_light(base_size = 14))


load('data/pegida_stm_posts.RData')
# ungrouping avoids confllicts due to older dplyr versions
prep10$data <- prep10$data %>% ungroup()
d <- out$meta %>% ungroup()



d$maxprop <- factor(d$maxprop,
                    levels = c(6, 1, 2, 3, 4, 5, 7, 8, 9, 10))



get_date <- function(x, startdate = "2014-12-29") {
  startdate <- ymd(startdate)
  x <- round(x, 0)
  return(startdate + x - 1)
}

replace_reg_str <- function(reg, string) {
  new_reg <- reg
  names(new_reg$coefficients) <-
    str_replace_all(names(new_reg$coefficients),
                    string, "")
  return(new_reg)
}

# recoding factor for topics
d$maxprop_str <-
  fct_recode(
    d$maxprop,
    "Protest demonstrations" = '9',
    "Refugee crisis in other countries" = '5',
    "Counter-demonstrations and page updates" = '10',
    "Anti-refugee content" = '4',
    "Islamization and assaults by foreigners" = '6',
    "Police operations" = '3',
    "Right-wing organizations in other countries" = '8',
    "FPÖ support" = '2',
    "Politics criticism" =  '1',
    "English" = '7'
  ) 
```


## Plots


### Figure 2

```{r, fig.height=5, fig.width=7}

plotTopicProps <- function(proportions,
                           fillcolor = "#377eb8") {
  # plot stm topic proportions
  names_ <- colnames(proportions)
  frequency <- colMeans(proportions)
  order <- order(frequency, decreasing = F)
  percentage <- frequency[order]
  names_ <- names_[order]
  topic <- factor(names_, levels = names_)
  combined <- data.frame(percentage, topic)
  combined <-
    transform(combined,
              mid_y = ave(
                combined$percentage,
                combined$topic,
                FUN = function(val)
                  cumsum(val) - (0.5 * val)
              ))
  combined$labels <-
    rev(
      c(
        "Protest demonstrations",
        "Refugee crisis in other countries",
        "Counter-demonstrations and page updates",
        "Anti-refugee content",
        "Islamization and assaults by foreigners",
        "Police operations",
        "Right-wing organizations in other countries",
        "FPÖ support",
        "Politics criticism",
        "English"
      )
    )
  p <-
    ggplot(combined, aes(x = topic, y = percentage , label = labels)) +
    geom_bar(stat = "identity",
             fill = fillcolor,
             color = "#000000") +
    coord_flip() + scale_y_continuous(labels = percent, expand = c(0, 0)) +
    labs(y = "Proportions", x = "Topics") +
    geom_text(aes(y = mid_y), colour = "white", size = 3.4) +
    theme(
      panel.grid.minor = element_blank(),
      panel.grid.major = element_line(
        size = 0.2,
        linetype = 'solid',
        colour = "grey90"
      )
    )
  
  return(p)
}


props <- posts10$theta # topic proportions
colnames(props) <- 1:ncol(props)

p <- plotTopicProps(props, fillcolor = 'grey40')
p

ggsave(
  "pegida_fig2.jpeg",
  p,
  width = 7,
  height = 5,
  dpi = 300,
  scale = 1
)
```



#### Figure 3


```{r}
effects <-
  get_effects(prep10, variable = 'day', type = 'continuous')  %>%
  mutate(
    day = get_date(value),
    label = as.factor(topic) %>%
      fct_recode(
        "Protest demonstrations" = '9',
        "Refugee crisis in other countries" = '5',
        "Counter-demonstrations and page updates" = '10',
        # and page updates
        "Anti-refugee content" = '4',
        "Islamization and assaults by foreigners" = '6',
        "Police operations" = '3',
        "Right-wing organizations in other countries" = '8',
        "FPÖ support" = '2',
        "Politics criticism" =  '1',
        "English" = '7'
      )
  )

p <- effects %>% filter(topic %in% c(6, 9)) %>%
  ggplot(aes(x = day, y = proportion, group = label)) +
  geom_line(aes(color = label, linetype = label)) +
  geom_ribbon(aes(ymin = lower, ymax = upper,
                  fill = label), alpha = 0.20) +
  theme(
    legend.position = c(0.7, 0.85),
    panel.grid.minor = element_blank(),
    panel.grid.major = element_line(
      size = 0.2,
      linetype = 'solid',
      colour = "grey90"
    ),
    legend.title = element_blank(),
    axis.text.x = element_text(
      angle = 90,
      hjust = 1,
      vjust = 0.5
    )
  ) +
  xlab("Date") + ylab("Topic Proportion") +
  scale_y_continuous(
    limits = c(0, .35),
    labels = scales::percent,
    expand = c(0, 0)
  ) +
  scale_x_date(date_breaks = "1 month",
               date_labels = "%y/%m",
               expand = c(0, 0)) +
  scale_fill_brewer(palette = 'Set1',
                    type = "qual",
                    guide = "legend")
p

ggsave(
  "pegida_fig3.jpeg",
  p,
  width = 7,
  height = 5,
  dpi = 300,
  scale = 1
)
```


### Figure 4

Regression models (see appendix for table)

```{r}
reg1 <- glm.nb(comments_count_fb ~ maxprop_str, data = d) %>%
  replace_reg_str("maxprop_str")
reg2 <-
  glm.nb(comments_count_fb ~ maxprop_str + day, data = d)  %>%
  replace_reg_str("maxprop_str")

reg3 <-
  glm.nb(comments_count_fb ~ maxprop_str + day * day + day * maxprop_str ,
         data = d)  %>%
  replace_reg_str("maxprop_str")


screenreg(list(reg1, reg2, reg3), digits = 3)

```

Figure

```{r, fig.height=5, fig.width=7}

mydf <- ggpredict(reg3, terms = c("day", "maxprop_str"))
mydf$day <- get_date(mydf$x)

p <-
  mydf %>% filter(group %in% c(
    "Islamization and assaults by foreigners",
    "Protest demonstrations"
  )) %>%
  ggplot(aes(day, predicted)) +
  geom_line(aes(color = group, linetype = group)) +
  geom_ribbon(aes(ymin = conf.low, ymax = conf.high, fill = group),
              alpha = 0.2)  +
  scale_y_continuous(
    breaks = c(0, 500, 1000, 1500, 2000),
    limits = c(0, 2000),
    expand = c(0, 0)
  ) +
  scale_x_date(date_breaks = "1 month",
               date_labels = "%y/%m",
               expand = c(0, 0)) +
  theme(
    panel.grid.minor = element_blank(),
    legend.position = c(0.7, 0.8),
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    axis.text.x = element_text(
      angle = 90,
      hjust = 1,
      vjust = 0.5
    ),
    panel.grid.major = element_line(
      size = 0.2,
      linetype = 'solid',
      colour = "grey90"
    )
  ) +
  labs(y = 'Predicted no. of comments', x = 'Date') +
  scale_fill_brewer(palette = 'Set1',
                    type = "qual",
                    guide = "legend")
p


ggsave(
  "pegida_fig4.jpeg",
  p,
  width = 7,
  height = 5,
  dpi = 300,
  scale = 1
)

```



## Other topics (see appendix)

```{r fig.height=9, fig.width=10}
o <- effects %>% filter(!topic %in% c(6, 9)) %>%
  ggplot(aes(x = day, y = proportion, group = label)) +
  
  geom_line(aes(color = "grey80")) +
  geom_ribbon(aes(ymin = lower, ymax = upper,
                  fill = "grey50"), alpha = 0.20) +
  theme(
    legend.position =  "none",
    panel.grid.minor = element_blank(),
    panel.grid.major = element_line(
      size = 0.2,
      linetype = 'solid',
      colour = "grey90"
    ),
    legend.title = element_blank(),
    axis.text.x = element_text(
      angle = 90,
      hjust = 1,
      vjust = 0.5
    )
  ) +
  xlab("Date") + ylab("Topic Proportion") +
  scale_y_continuous(labels = scales::percent,
                     expand = c(0, 0)) +
  scale_x_date(date_breaks = "1 month",
               date_labels = "%y/%m",
               expand = c(0, 0)) +
  
  facet_wrap('~label')
o


ggsave(
  "pegida_suppfig_othertopics.jpeg",
  o,
  width = 10,
  height = 9,
  dpi = 300,
  scale = 1
)

```




